bellhop | 您所在的位置:网站首页 › iframe send message to parent › bellhop |
Bellhop
Bellhop is a simple event-based communication layer between the page DOM and an iframe. It doesn't require any additional dependencies. Super easy to use and setup. Installation npm install bellhop-iframe Importing BellhopThe Bellhop module contains support for ES6 modules, CommonJS and browser global definitions. To import with ES6, import { Bellhop } from 'bellhop-iframe';To import with CommonJS, refer instead to the UMD build const Bellhop = require('bellhop-iframe/dist/bellhop-umd.js');You can also import the UMD version by using import import 'bellhop-iframe/dist/bellhop-umd.js';Lastly, the UMD module can also be directly included on an HTML page. This will declare Bellhop and attach it directly to window Basic UsageHere's a very simple example to get started. We have two pages index.html and child.html. This is the minimum you need to get them talking to each other. Contents of index.html Contents of child.html Available Methods new BellhopThe constructor creates a new Bellhop instance, taking an optional unique identifier for this instance. If no id is provided, a random one is selected connectConnects a Bellhop instance to an iframe, or it's containing window. For instance, given a Bellhop instance bellhop: bellhop.connect();will connect a child iframe to it's parent, allowing it to emit messages out of the iframe. However, var iframe = document.querySelector('iframe'); bellhop.connect(iframe);allows a containing page to connect with an interior iframe and emit message into the iframe. destroydisconnect removes any listener for events from another frame, and stops listening for messages altogether offRemoves an event listener previously added by the .on() method, or removes a given callback method from a listener. When deleting a callback, the function passed in is required to be the original function passed into the .on() method. bellhop.off(‘init’); // removes the listener ‘init’ and all callbacks assigned to it bellhop.off(‘init’, callback) // removes the specific callback provided without removing the listener sendSends a named message to another iframe: bellhop.send('newHighscore', { value: 100 }); fetch and respondConvenience methods for automating response of values between the interior and exterior of frames. For instance: // index.html var iframe = document.querySelector('iframe'); var bellhop = new Bellhop(iframe); bellhop.connect(); bellhop.respond('config', { difficulty: 'hard', theme: 'dark' }); // child.html var bellhop = new Bellhop(); bellhop.connect(); bellhop.fetch('config', function(result) { console.log(result); // { difficulty: 'hard', theme: 'dark' } });Additionally, object passed to respond() can be a function, whose result will be returned in the callback of the fetch function. // index.html var functionExample = function(){ return "result of functionExample"; }; bellhop.respond('function', functionExample); // child.html bellhop.fetch('function', function(result) { console.log(result.data); //result of functionExample });Furthermore, respond() accepts a plain object, string, or number. If a function is passed, it will be called and the function's return-value sent. If a promise is passed in or returned from a function that was passed in, that promise will be await-ed before it's value returned. For example, the following all return "data" to bellhop.fetch() //(example) bellhop.respond('example', "data"); //OR (promise example) let promiseData = new Promise(function(resolve, reject) { resolve("data") }); bellhop.respond('example', promiseData) //OR (function example) var functionExample = function(){ return "data"; }; bellhop.respond('example', functionExample); //OR (function that returns a promise) var functionPromiseExample = function(){ return new Promise(function(resolve, reject) { resolve("data") }); }; bellhop.respond('example', functionPromiseExample); triggerTriggers any event handlers for a given event type optionally passing data to other areas in the app that are listening for this event bellhop.trigger('eventType', {data: 'example'}); // triggers the event 'eventType' passing data to it's handlers Debug ModeBellhop has a debug mode which enables additional logging when an instance sends or receives a message. It can be enabled by simply setting the debug flag to true: bellhop.debug = true;By default (above method) it will print a message outlining whether the bellhop instance was a child or parent, whether the message was sent or received, and the contents of the message. If you require additional or custom logging you can also pass a function as the flag. const log = () =; {console.log('Hello World!');} bellhop.debug = log; // Hello World!If you pass a function to debug three parameters* are passed to help fill out the log statements if required: const log = ({isChild, received, message}) =; { console.log(isChild); // (boolean) whether the instance is a child or parent. console.log(received); // (boolean) whether the instance has received a message or sent one. console.log(message); // (object) the content of the message. }*Note: the names must be identical, but you are able to omit any or all if they're not required. targetProperty for retrieving the iframe element through which this Bellhop instance is communicating: var iframe = document.querySelector('iframe'); var bellhop = new Bellhop(iframe); console.log(bellhop.target === iframe.contentWindow); // true LicenseCopyright (c) 2021 Springroll Released under the MIT License. |
CopyRight 2018-2019 实验室设备网 版权所有 |